class BlackDeathZombieSpawn extends NavigationPoint
	Placeable;

var() bool bEnabled;//Enable this Spawn?
var() class<BlackDeathPawn>				PawnType;//Which Enemy do you want to Spawn?
var() class<BlackDeathEnemyController>  ControllerType;//Temp fix, explicitly tells which AI to use for the pawn
var() int SpawnNumber;//How many do we Spawn?
var() float SpawnDelay; //delay between spawns
var() bool bInitialDelay; //Apply the delay before the first spawn?

event PostBeginPlay()
{
	Super.PostBeginPlay();
	if(SpawnNumber > 1 && SpawnDelay == 0)//check if no dealy for multiple spawns; 
	{
		SpawnDelay = 0.1;//set it to not be 0 or we'd have a problem
	}
}

function BlackDeathPawn SpawnZombie()//spawn and initialize the new zombie
{
	local BlackDeathPawn NewPawn;
	local BlackDeathEnemyController Controller;
	NewPawn = Spawn(PawnType);
	if(NewPawn != none)
	{
		if(NewPawn.Controller == none)
		{
			// give the pawn a controller here, since we don't have one
			Controller = NewPawn.Spawn(ControllerType,,, NewPawn.Location, NewPawn.Rotation);
			if ( Controller != None )
			{
				// handle the team assignment
				//Controller.SetTeam(TeamIndex);
				// force the controller to possess, etc
				Controller.Possess(newPawn, false);
			}
			else//then the controller didn't spawn properly
			{
				NewPawn.destroy();//get rid of the pawn, he has no controller
			}
		}
		
		// create any inventory
		if (WorldInfo.Game != None)
		{
			WorldInfo.Game.AddDefaultInventory(NewPawn);
		}
	}
	return NewPawn;
}

/** call this function to ignore the spawn requirements and just force a zombie to spawn */
function ForceSpawn()
{
	SpawnZombie();
}

function bool CheckValidSpawn()//Check to make sure its safe to put a zombie here
{
	local Actor A;
	ForEach CollidingActors(class'Actor', A, 100)//if there is an overlapping actor, dont spawn
	{
		return false;
	}
	return true;
}

simulated function PrepareSpawn()//called to tell a spawn to check the area and spawn a zombie if it can
{
	local BlackDeathPawn NewPawn;
	//`log("check for valid spawn");
	if(CheckValidSpawn())
	{
		NewPawn = SpawnZombie();
		if(NewPawn != none)
		{
			SpawnNumber -= 1;//decrement spawn count
		}
	}
	if(SpawnNumber <=0)//If we spawned everyone
	{
		goToState('inactive');//put spawner into inactive for now since its done
	}
}

auto state Active
{
	simulated event BeginState(Name PreviousStateName)
	{
		if(!bEnabled)//if its not enabled, move it immediatly to inactive and wait
		{
			goToState('inactive');
		}
		else
		{
			//`log("begin active for spawn");
			if(SpawnDelay <= 0.0 || !bInitialDelay)
			{
				//`log("No delay");
				PrepareSpawn();
			}
			if(SpawnNumber > 0)
			{
				//`log("SpawnNum > 0, setting timer");
				SetTimer(SpawnDelay, true, 'PrepareSpawn');
			}
			else
			{
				//`log("goign inactive");
				goToState('Inactive');
			}
		}
	}

	simulated event EndState(Name NextStateName)
	{
		ClearTimer('PrepareSpawn');
	}
}

state Inactive
{
	simulated event BeginState(Name PreviousStateName)
	{
	}
	
	simulated function PrepareSpawn();//No spawning in inactive mode (we can still force a spawn though).

	simulated event EndState(Name NextStateName)
	{
	}
}

function OnToggle(SeqAct_Toggle inAction)//controls the kismet Toggle node input
{
	if (inAction.InputLinks[0].bHasImpulse)
	{
		bEnabled = true;
	}
	else if (inAction.InputLinks[1].bHasImpulse)
	{
		bEnabled = false;
	}
	else if (inAction.InputLinks[2].bHasImpulse)
	{
		bEnabled = !bEnabled;
		
	}
	goToState('Active');
	WorldInfo.Game.NotifyNavigationChanged(self);

	bShouldSaveForCheckpoint = true;
}

defaultproperties
{
	ControllerType = class'BlackDeathEnemyController'
	PawnType = class'BlackDeathZombiePawn'
	
	bEnabled = true
	bInitialDelay = false
	SpawnDelay = 0.0
	SpawnNumber = 1
	
	bStatic = false
	bNoDelete=true
}
